package com.caiyouxi.finance.service;

import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.common.util.Log;
import com.caiyouxi.finance.mapper.CouponTypeMapper;
import com.caiyouxi.finance.model.CouponType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;


/**
 * 券种类
 * chensong
 */
@Service
public class CouponTypeService extends BaseService {
    
    @Autowired
    private CouponTypeMapper couponTypeMapper;

    public CouponType select_coupon_type(Integer couponTypeId){
        String key = "coupon_type_" + couponTypeId;
        CouponType ct = (CouponType) redisService.get(key);
        if(ct == null){
            ct = couponTypeMapper.select_coupon_type(couponTypeId);
            redisService.set(key,ct);
        }
        return ct;
    }

    public void add_coupon_type(CouponType couponType){
        couponTypeMapper.add_coupon_type(couponType);
        Log.d("保存券种类信息结束");
    }

    public void update_coupon_type(CouponType couponType){
        couponTypeMapper.update_coupon_type(couponType);
    }

    public void delete_coupon_type(CouponType couponType){
        couponTypeMapper.delete_coupon_type(couponType);
    }
    
    /**
     * 券种类校验
     */
    @Transactional
    public boolean check_coupon_type(CouponType couponType,String fund_type){
        //状态。0-停用；1-启用
        if(1 != couponType.getEnabled()){
            //券种类未启用
            Log.d("券种类未启用");
            return false;
        }
        if(!fund_type.equals(couponType.getFund_type())){
            //券种类资金用途不符
            Log.d("券种类资金用途不符");
            return false;
        }
        return true;
    }

    /**
     * 根据券ID和fund_type查询对应的券种类
     */
    public List<CouponType> select_coupon_type_by_coupons_ids(String[] coupons_ids, String fund_type){
        HashMap<String,Object> map = new HashMap();
        map.put("coupons_ids",coupons_ids);
        map.put("fund_type",fund_type);
        return couponTypeMapper.select_coupon_type_by_coupons_ids(map);
    }
}
